package com.zb.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import javax.swing.*;
import java.beans.PropertyVetoException;

/**
 * @Author: 张波
 * @Time: 2025/8/19 19:46
 */
@Data
@Configuration
@ComponentScan("com.zb")
@Import({DataSourceConfig.class})
public class SpringConfiguration {
	@Value("hello这是注解配置")
	private String configName;

	@Autowired
	private DataSourceConfig dataSourceConfig;

	public DataSource getMyDruidDatasource(){
		// 获取 Druid 数据源
		DruidDataSource druidDataSource = new DruidDataSource();
		try {
			druidDataSource.setDriverClassName(dataSourceConfig.getDriver());
			druidDataSource.setUrl(dataSourceConfig.getUrl());
			druidDataSource.setUsername(dataSourceConfig.getUsername());
			druidDataSource.setPassword(dataSourceConfig.getPassword());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return druidDataSource;
	}
	/// 获取C3p0数据源
	@Bean(name = "getC3P0Datasource")
	public DataSource getMyC3P0Datasource(){
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		try {
			comboPooledDataSource.setDriverClass(dataSourceConfig.getDriver());
			comboPooledDataSource.setJdbcUrl(dataSourceConfig.getUrl());
			comboPooledDataSource.setUser(dataSourceConfig.getUsername());
			comboPooledDataSource.setPassword(dataSourceConfig.getPassword());
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		return comboPooledDataSource;
	}
}
